/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BuildingDetailPanel.java
 *
 * Created on 15 Feb 2011, 2:18:34 PM
 */
package za.co.pas.spaceandtime.view.panel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import za.co.pas.business.abstracts.AbstractView;
import za.co.pas.business.constant.Constant;
import za.co.pas.business.data.Address;
import za.co.pas.business.message.MessageObj;
import za.co.pas.business.view.dlg.AddressDialog;
import za.co.pas.business.view.panel.AddressPanel;
import za.co.pas.spaceandtime.controller.BuildingDetailController;
import za.co.pas.spaceandtime.data.Building;
import za.co.pas.spaceandtime.data.Room;
import za.co.pas.spaceandtime.message.MsgConstant;
import za.co.pas.spaceandtime.model.list.RoomListModel;
import za.co.pas.spaceandtime.view.dlg.RoomDialog;

/**
 *
 * @author alabuschagne
 */
public class BuildingDetailPanel extends JPanel implements AbstractView
{

    private BuildingDetailController buildingDetailController;
    private Building newBuilding;
    private boolean dirty = false;
    private RoomListModel roomListModel = new RoomListModel();

    private BuildingDetailPanel()
    {
    }

    /** Creates new form BuildingDetailPanel */
    public BuildingDetailPanel(BuildingDetailController buildingDetailController)
    {
        this.buildingDetailController = buildingDetailController;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        companyNamePanel = new javax.swing.JPanel();
        buildingNameTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        buildingDescriptionTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        buildingEnabledCheckBox = new javax.swing.JCheckBox();
        companyAddressPanel = new javax.swing.JPanel();
        streetAddressLabel = new javax.swing.JLabel();
        postalAddressLabel = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        editCompanyStreetAddressButton = new javax.swing.JButton();
        editCompanyPostalAddressButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        roomsList = new javax.swing.JList();

        companyNamePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Name", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        buildingNameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                buildingNameTextFieldKeyPressed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Building name:");

        buildingDescriptionTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                buildingDescriptionTextFieldKeyPressed(evt);
            }
        });

        jLabel2.setText("Description:");

        buildingEnabledCheckBox.setText("Enabled");
        buildingEnabledCheckBox.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);

        javax.swing.GroupLayout companyNamePanelLayout = new javax.swing.GroupLayout(companyNamePanel);
        companyNamePanel.setLayout(companyNamePanelLayout);
        companyNamePanelLayout.setHorizontalGroup(
            companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(companyNamePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(58, 58, 58)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buildingNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, companyNamePanelLayout.createSequentialGroup()
                        .addComponent(buildingDescriptionTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(buildingEnabledCheckBox)))
                .addContainerGap())
        );
        companyNamePanelLayout.setVerticalGroup(
            companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(companyNamePanelLayout.createSequentialGroup()
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(buildingNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buildingDescriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(buildingEnabledCheckBox))
                .addContainerGap())
        );

        companyAddressPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Address", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        streetAddressLabel.setToolTipText("Right click for menu");
        streetAddressLabel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        streetAddressLabel.setOpaque(true);
        streetAddressLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                streetAddressLabelMouseReleased(evt);
            }
        });

        postalAddressLabel.setToolTipText("Right click for menu");
        postalAddressLabel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        postalAddressLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                postalAddressLabelMouseReleased(evt);
            }
        });

        jLabel13.setText("Street address:");

        jLabel14.setText("Postal address:");

        editCompanyStreetAddressButton.setText("...");
        editCompanyStreetAddressButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editCompanyStreetAddressButtonActionPerformed(evt);
            }
        });

        editCompanyPostalAddressButton.setText("...");
        editCompanyPostalAddressButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editCompanyPostalAddressButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout companyAddressPanelLayout = new javax.swing.GroupLayout(companyAddressPanel);
        companyAddressPanel.setLayout(companyAddressPanelLayout);
        companyAddressPanelLayout.setHorizontalGroup(
            companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, companyAddressPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(postalAddressLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                    .addComponent(streetAddressLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(editCompanyPostalAddressButton)
                    .addComponent(editCompanyStreetAddressButton))
                .addContainerGap())
        );
        companyAddressPanelLayout.setVerticalGroup(
            companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(companyAddressPanelLayout.createSequentialGroup()
                .addGroup(companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel13)
                    .addComponent(streetAddressLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editCompanyStreetAddressButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(editCompanyPostalAddressButton)
                    .addGroup(companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(postalAddressLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel14, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Rooms", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
        jPanel1.setLayout(new java.awt.BorderLayout());

        roomsList.setModel(roomListModel);
        roomsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                roomsListMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(roomsList);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(companyNamePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(companyAddressPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(companyNamePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(companyAddressPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(75, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buildingNameTextFieldKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_buildingNameTextFieldKeyPressed
    {//GEN-HEADEREND:event_buildingNameTextFieldKeyPressed
        dirty = true;
}//GEN-LAST:event_buildingNameTextFieldKeyPressed

    private void buildingDescriptionTextFieldKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_buildingDescriptionTextFieldKeyPressed
    {//GEN-HEADEREND:event_buildingDescriptionTextFieldKeyPressed
        dirty = true;
}//GEN-LAST:event_buildingDescriptionTextFieldKeyPressed

    private void streetAddressLabelMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_streetAddressLabelMouseReleased
    {//GEN-HEADEREND:event_streetAddressLabelMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("Copy to postal address");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    if (newBuilding == null)
                    {
                        newBuilding = new Building();
                    }
                    newBuilding.setPostalAddress(newBuilding.getStreetAddress());
                    postalAddressLabel.setText(newBuilding.getPostalAddress().toString());
                }
            });
            menuItem = new JMenuItem("Edit");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editCompanyStreetAddressButtonActionPerformed(null);
                }
            });

            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
}//GEN-LAST:event_streetAddressLabelMouseReleased

    private void postalAddressLabelMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_postalAddressLabelMouseReleased
    {//GEN-HEADEREND:event_postalAddressLabelMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("Copy to street address");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    if (newBuilding == null)
                    {
                        newBuilding = new Building();
                    }
                    newBuilding.setStreetAddress(newBuilding.getPostalAddress());
                    streetAddressLabel.setText(newBuilding.getStreetAddress().toString());
                }
            });
            menuItem = new JMenuItem("Edit");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editCompanyPostalAddressButtonActionPerformed(null);
                }
            });

            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
}//GEN-LAST:event_postalAddressLabelMouseReleased

    private void editCompanyStreetAddressButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editCompanyStreetAddressButtonActionPerformed
    {//GEN-HEADEREND:event_editCompanyStreetAddressButtonActionPerformed
        if (newBuilding == null)
        {
            newBuilding = new Building();
        }
        AddressDialog ad = new AddressDialog(Constant.getParentFrame(this), true, "Street adress", "Street address 1:", "Street address 2:");
        ad.setAddress(newBuilding.getStreetAddress());
        ad.setVisible(true);
        if (ad.isOk())
        {
            AddressPanel ap = ad.getAddressPanel();
            Address sAdd = newBuilding.getStreetAddress();
            if (sAdd == null)
            {
                sAdd = new Address();
                newBuilding.setStreetAddress(sAdd);
            }
            sAdd.setCity(ap.getCity());
            sAdd.setCode(ap.getCode());
            sAdd.setCountry(ap.getCountry());
            sAdd.setLine1(ap.getStreet1());
            sAdd.setLine2(ap.getStreet2());
            sAdd.setProvince(ap.getProvince());
            sAdd.setSuburb(ap.getSuburb());
            streetAddressLabel.setText(sAdd.toString());
        }
}//GEN-LAST:event_editCompanyStreetAddressButtonActionPerformed

    private void editCompanyPostalAddressButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editCompanyPostalAddressButtonActionPerformed
    {//GEN-HEADEREND:event_editCompanyPostalAddressButtonActionPerformed
        if (newBuilding == null)
        {
            newBuilding = new Building();
        }
        AddressDialog ad = new AddressDialog(Constant.getParentFrame(this), true, "Postal adress", "Postal address 1:", "Postal address 2:");
        ad.setAddress(newBuilding.getPostalAddress());
        ad.setVisible(true);
        if (ad.isOk())
        {
            AddressPanel ap = ad.getAddressPanel();
            Address pAdd = newBuilding.getPostalAddress();
            if (pAdd == null)
            {
                pAdd = new Address();
                newBuilding.setPostalAddress(pAdd);
            }
            pAdd.setCity(ap.getCity());
            pAdd.setCode(ap.getCode());
            pAdd.setCountry(ap.getCountry());
            pAdd.setLine1(ap.getStreet1());
            pAdd.setLine2(ap.getStreet2());
            pAdd.setProvince(ap.getProvince());
            pAdd.setSuburb(ap.getSuburb());
            postalAddressLabel.setText(pAdd.toString());
        }
}//GEN-LAST:event_editCompanyPostalAddressButtonActionPerformed

    private void roomsListMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_roomsListMouseReleased
    {//GEN-HEADEREND:event_roomsListMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("New");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editRoomDetails(null);
                }
            });
            int k = roomsList.getSelectedIndex();
            if (k != -1)
            {
                menuItem = new JMenuItem("Edit");
                Pmenu.add(menuItem);
                menuItem.addActionListener(new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        int k = roomsList.getSelectedIndex();
                        if (k != -1)
                        {
                            Room room = (Room) roomListModel.getElementAt(k);
                            editRoomDetails(room);
                        }
                    }
                });
            }
            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_roomsListMouseReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField buildingDescriptionTextField;
    private javax.swing.JCheckBox buildingEnabledCheckBox;
    private javax.swing.JTextField buildingNameTextField;
    private javax.swing.JPanel companyAddressPanel;
    private javax.swing.JPanel companyNamePanel;
    private javax.swing.JButton editCompanyPostalAddressButton;
    private javax.swing.JButton editCompanyStreetAddressButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel postalAddressLabel;
    private javax.swing.JList roomsList;
    private javax.swing.JLabel streetAddressLabel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt)
    {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (evt.getPropertyName().compareTo(MsgConstant.REQUEST_BUILDING_DETAIL_CHANGED) == 0)
        {
            if ((evt.getNewValue() != null) && (evt.getNewValue() instanceof Building))
            {
                Building currentBuilding = (Building) evt.getOldValue();
                Building sendNewBuilding = (Building) evt.getNewValue();
                if (checkBuilding(currentBuilding) == JOptionPane.NO_OPTION)
                {
                    try
                    {
                        newBuilding = sendNewBuilding.clone();
                        buildingDetailController.setCurrentBuilding(sendNewBuilding);
                    }
                    catch (Exception ex)
                    {
                        ex.printStackTrace();
                    }
                    updateBuildingFields();
                }
            }
        }
        else if (evt.getPropertyName().compareTo(MessageObj.COMPANY_DETAILS_CHANGED) == 0)
        {
            System.out.println("Maybe handle this???!!!");
        }
    }

    private void editRoomDetails(Room r)
    {
        RoomDialog rd = new RoomDialog(Constant.getParentFrame(this), true);
        if (r != null)
        {
            rd.setRoom(r);
        }
        rd.setVisible(true);
        if (rd.isOk())
        {
            Room r2 = rd.getRoom();
            if (r == null)
            {
                newBuilding.addRoom(r2);
            }
            else
            {
                r.setBeds(r2.getBeds());
                r.setDescription(r2.getDescription());
                r.setEnabled(r2.getEnabled());
                r.setMaxCapacity(r2.getMaxCapacity());
                r.setName(r2.getName());
                r.setRoomType(r2.getRoomType());
                r.setShareBeds(r2.getShareBeds());
            }
        }
    }

    private void updateBuildingFields()
    {

        if (newBuilding == null)
        {
            newBuilding = new Building();
        }
        buildingEnabledCheckBox.setSelected(newBuilding.getEnabled());
        this.buildingNameTextField.setText(newBuilding.getName());
        this.buildingDescriptionTextField.setText(newBuilding.getDescription());
        if (newBuilding.getStreetAddress() != null)
        {
            this.streetAddressLabel.setText(newBuilding.getStreetAddress().toString());
        }
        else
        {
            this.streetAddressLabel.setText("");
        }
        if (newBuilding.getPostalAddress() != null)
        {
            this.postalAddressLabel.setText(newBuilding.getPostalAddress().toString());
        }
        else
        {
            this.postalAddressLabel.setText("");
        }
        this.roomListModel.setRooms(newBuilding.getRooms());

        this.roomsList.updateUI();
        setDirty(false);
    }

    private void updateData()
    {
        if (newBuilding == null)
        {
            return;
        }
        newBuilding.setName(buildingNameTextField.getText().trim().length() == 0 ? null : buildingNameTextField.getText().trim());
        newBuilding.setDescription(buildingDescriptionTextField.getText().trim().length() == 0 ? null : buildingDescriptionTextField.getText().trim());
        newBuilding.setEnabled(buildingEnabledCheckBox.isSelected());
    }

    @Override
    public boolean isDirty()
    {
        return dirty;
    }

    public void setDirty(boolean b)
    {
        dirty = b;
    }

    private int checkBuilding(Building building)
    {
        //first set the text field data to newCompany fields
        updateData();
        if ((newBuilding == null) && (!isDirty()))
        {
            return JOptionPane.NO_OPTION;
        }
        else if ((newBuilding == null) && (isDirty()))
        {
            int resp = JOptionPane.showConfirmDialog(this, "Save changes?", "Unsaved data", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (resp == JOptionPane.YES_OPTION)
            {
                buildingDetailController.SaveBuilding(newBuilding);
            }
            return resp;
        }
        else if ((newBuilding != null) && (!newBuilding.check(building)))
        {
            int resp = JOptionPane.showConfirmDialog(this, "Save changes?", "Unsaved data", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (resp == JOptionPane.YES_OPTION)
            {
                buildingDetailController.SaveBuilding(newBuilding);
            }
            return resp;
        }
        return JOptionPane.NO_OPTION;
    }

    @Override
    public String getExtraTitle()
    {
        if (newBuilding != null)
        {
            return newBuilding.getName();
        }
        else
        {
            return " [NEW] ";
        }
    }
}
